﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using DataLayer;
using ViewModel;

namespace Service
{
    public class Announcer
    {
        public static void SetAnnouncerIp(string ip)
        {
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    var server = DB.appsettings.Where(x => x.name == "announcer_ip").FirstOrDefault();
                    server.value = ip;
                    DB.SaveChanges();

                    var port = DB.appsettings.Where(x => x.name == "port").FirstOrDefault();
                    ActiveSession.Port = Convert.ToInt32(port.value);

                    var delayInterval = DB.appsettings.Where(x => x.name == "announcer_check_interval").FirstOrDefault();
                    ActiveSession.AnnouncerDelayInterval = Convert.ToInt32(delayInterval.value);
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("SetAnnouncerIP", ex);
                throw;
            }

        }

        public static List<int> GetItemsToAnnounce()
        {
            List<int> ids = new List<int>();
            try
            {
                DateTime dt = DateTime.Now.AddMinutes(-30);
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    ids = DB.announcers.Where(x => x.datetime >= dt).Select(y => y.issue_id).ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetItemsToAnnounce", ex);
                throw;
            }
            return ids;
        }

        public static List<Issue> GetItemsToBeNotified(List<int> issueIds)
        {
            List<Issue> issues = new List<Issue>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    issues = (from a in DB.assignments.Where(x => issueIds.Contains(x.issue_id)).ToList()
                              join s in DB.shops on a.shop_id equals s.id
                              join current in DB.activesessions on a.shop_id equals current.user_id
                              where current.is_active
                              select new Issue()
                              {
                                  ID = a.issue_id,
                                  Assignee = new List<Shop>() { new Shop() { ID = s.id, UDF5 = current.machine_ip } },
                              })
                         .ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetItemsToBeNotified", ex);
                throw;
            }

            return issues;
        }

        public static void RemoveItemsAfterNotified(List<int> issueIds)
        {
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    foreach (var item in issueIds)
                    {
                        var o = DB.announcers.Where(x => x.issue_id == item).FirstOrDefault();
                        DB.announcers.Remove(o);
                    }
                    DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("RemoveItemsAfterNotified", ex);
                throw;
            }
        }

    }
}
